<template>
  <div class="app-container">
    <el-form ref="hotRef" :model="form" label-width="80px">
      <el-form-item>
        <el-row :gutter="10" style="width: 100%">
          <el-col :span="8">
            <el-form-item label="用户id">
              <el-text class="mx-1" type="danger">{{ form.userId }}</el-text>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="股票名称">
              <el-text class="mx-1" type="danger">{{ form.shareName }}</el-text>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="股票代码">
              <el-text class="mx-1" type="danger">{{ form.shareCode }}</el-text>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10" style="width: 100%">
          <el-col :span="8">
            <el-form-item label="活跃度">
              <el-text class="mx-1" type="danger">{{ form.activity }}</el-text>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="核心题材">
              <el-text class="mx-1" type="danger">{{ form.boardNameKey }}</el-text>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="地区">
              <el-text class="mx-1" type="danger">{{ form.area }}</el-text>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form-item>

      <el-form-item class="text-wrapper">
        <el-row :gutter="10">
          <el-col :span="24">
            <el-form-item label="题材" :align='center'>
              <div v-html="handleTC(form.boardName)"></div>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="hotList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="编号" align="center" prop="id"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="用户id" align="center" prop="userId"/>
      <el-table-column label="数字代码" align="center" prop="shareCode"/>
      <el-table-column label="名称" align="center" prop="shareName"/>
      <el-table-column label="活跃度" align="center" prop="activity">
        <template #default="scope">
          <dict-tag :options="com_activity" :value="scope.row.activity"/>
        </template>
      </el-table-column>
      <el-table-column label="题材" align="center" prop="boardName"/>
      <el-table-column label="核心题材" align="center" prop="boardNameKey"/>
      <el-table-column label="地区" align="center" prop="area"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="tickets" @click="handleDetail(scope.row)"
                     v-hasPermi="['enterprise:hot:list']">详情
          </el-button>
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['enterprise:hot:edit']">修改
          </el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['enterprise:hot:remove']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改热门股票对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="hotRef" :model="form" :rules="rules" label-width="80px">
<!--        <el-form-item label="用户id" prop="userId">-->
<!--          <el-input v-model="form.userId" placeholder="请输入用户id"/>-->
<!--        </el-form-item>-->
        <el-form-item label="数字代码" prop="shareCode">
          <el-input v-model="form.shareCode" placeholder="请输入数字代码"/>
        </el-form-item>
        <el-form-item label="名称" prop="shareName">
          <el-input v-model="form.shareName" placeholder="请输入名称"/>
        </el-form-item>
        <el-form-item label="活跃度" prop="activity">
          <el-select v-model="form.activity" placeholder="请选择活跃度">
            <el-option
                v-for="dict in com_activity"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="题材" prop="boardName">
          <el-input v-model="form.boardName" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="核心题材" prop="boardNameKey">
          <el-input v-model="form.boardNameKey" placeholder="请输入核心题材"/>
        </el-form-item>
        <el-form-item label="地区" prop="area">
          <el-input v-model="form.area" placeholder="请输入地区"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Hot">
import {listHot, getHot, delHot, addHot, updateHot} from "@/api/enterprise/hot";
import {useRouter} from 'vue-router';

const router = useRouter();
const {proxy} = getCurrentInstance();
const {com_activity} = proxy.useDict('com_activity');

const hotList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({

  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    userId: null,
    shareCode: null,
    shareName: null,
    activity: null,
    boardName: null,
    boardNameKey: null,
    area: null
  },
  rules: {
    updateTime: [
      {required: true, message: "更新时间不能为空", trigger: "blur"}
    ],
  }
});

const {queryParams, form, rules} = toRefs(data);

/** 查询热门股票列表 */
function getList() {
  loading.value = true;
  listHot(queryParams.value).then(response => {
    hotList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 查询热门股票列表 */

const handleDetail = (scope) => {
  router.push({path: '/enterprise/hot/' + scope.id});
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    createTime: null,
    updateTime: null,
    userId: null,
    shareCode: null,
    shareName: null,
    activity: null,
    boardName: null,
    boardNameKey: null,
    area: null
  };
  proxy.resetForm("hotRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加热门股票";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getHot(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改热门股票";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["hotRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateHot(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addHot(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除热门股票编号为"' + _ids + '"的数据项？').then(function () {
    return delHot(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('enterprise/hot/export', {
    ...queryParams.value
  }, `hot_${new Date().getTime()}.xlsx`)
}

getList();
</script>
